package com.pig4cloud.pig.module.controller.charger;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.OkHttpClientUtil;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.charger.ChargerPowerRole;
import com.pig4cloud.pig.module.api.entity.location.City;
import com.pig4cloud.pig.module.api.entity.location.District;
import com.pig4cloud.pig.module.api.entity.location.Province;
import com.pig4cloud.pig.module.service.charger.IChargerPowerRoleService;
import com.pig4cloud.pig.module.service.location.CityService;
import com.pig4cloud.pig.module.service.location.DistrictService;
import com.pig4cloud.pig.module.service.location.ProvinceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 充电设备收费规则Controller
 *
 * @author ruoyi
 * @date 2022-06-29
 */
@Validated
@Api(value = "充电设备收费规则控制器", tags = {"充电设备收费规则管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/charger/power/role")
public class ChargerPowerRoleController {

    private final IChargerPowerRoleService chargerPowerRoleService;
	private final ProvinceService provinceService;
	private final CityService cityService;
	private final DistrictService districtService;

    /**
     * 查询充电设备收费规则列表
     */
    @ApiOperation("查询充电设备收费规则列表")
    @PreAuthorize("@pms.hasPermission('module:powerRole:list')")
    @GetMapping("/list")
    public TableDataInfo<ChargerPowerRole> list(@Validated ChargerPowerRole chargerPowerRole) {
        QueryWrapper<ChargerPowerRole> qw = new QueryWrapper<>();
        if(chargerPowerRole.getRoleType() != null){
            qw.eq("role_type", chargerPowerRole.getRoleType());
        }
        if(chargerPowerRole.getEstateId() != null){
            qw.eq("estate_id", chargerPowerRole.getEstateId());
        }

        TableDataInfo<ChargerPowerRole> td = PageUtils.buildDataInfo(chargerPowerRoleService.page(PageUtils.buildPage(),qw));
        List<ChargerPowerRole> list = td.getData();
        for (ChargerPowerRole powerRole : list){
			Province province = provinceService.findById(powerRole.getProv());
			if(province!=null){
				powerRole.setProvName(province.getProvinceName());
			}

			City city = cityService.findById(powerRole.getCity());
			if(city != null){
				powerRole.setCityName(city.getCityName());
			}

			District district = districtService.findById(powerRole.getDistrict());
			if(district != null){
				powerRole.setDistrictName(district.getDistrictName());
			}
		}
        return td;
    }

    /**
     * 获取充电设备收费规则详细信息
     */
    @ApiOperation("获取充电设备收费规则详细信息")
    @PreAuthorize("@pms.hasPermission('module:powerRole:query')")
    @GetMapping("/{id}")
    public R<ChargerPowerRole> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        ChargerPowerRole chargerPowerRole = chargerPowerRoleService.getById(id);
        if(chargerPowerRole!=null){
			String[] areas = { chargerPowerRole.getProv(),chargerPowerRole.getCity(),chargerPowerRole.getDistrict() };
			chargerPowerRole.setProvinceArray(areas);
		}
        return R.ok(chargerPowerRole);
    }

    /**
     * 新增充电设备收费规则
     */
    @ApiOperation("新增充电设备收费规则")
    @PreAuthorize("@pms.hasPermission('module:powerRole:add')")
    @SysLog("充电设备收费规则")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody ChargerPowerRole chargerPowerRole) {
        boolean bl = chargerPowerRoleService.save(chargerPowerRole);
        if(bl){
            OkHttpClientUtil.getInstance().getData("https://www.sdyuedou.com/capi/charger/reset/power/role/" + chargerPowerRole.getRoleType());
        }
        return R.okByRow(bl ? 1 : 0);
    }

    /**
     * 修改充电设备收费规则
     */
    @ApiOperation("修改充电设备收费规则")
    @PreAuthorize("@pms.hasPermission('module:powerRole:edit')")
    @SysLog("充电设备收费规则")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody ChargerPowerRole chargerPowerRole) {
        boolean bl = chargerPowerRoleService.updateById(chargerPowerRole) ;
        if(bl){
            OkHttpClientUtil.getInstance().getData("https://www.sdyuedou.com/capi/charger/reset/power/role/" + chargerPowerRole.getRoleType());
        }
        return R.okByRow(bl ? 1 : 0);
    }

    /**
     * 删除充电设备收费规则
     */
    @ApiOperation("删除充电设备收费规则")
    @PreAuthorize("@pms.hasPermission('module:powerRole:remove')")
    @SysLog("充电设备收费规则")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(chargerPowerRoleService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
